﻿<UserControl x:Class="FiveInARow.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			 xmlns:comp="clr-namespace:FiveInARow.Components"
			 xmlns:gl="clr-namespace:GameLogic;assembly=GameLogic"
			 xmlns:conv="clr-namespace:FiveInARow.Converters"
             mc:Ignorable="d" 
             >
	
	<UserControl.Resources>
		<conv:RowLocationConverter x:Key="RowLocationConverter" />
        <conv:CellValueConverter x:Key="CellValueConverter" />
        <conv:CP2VisibilityConverter x:Key="CP2VisibilityConverter" />
	</UserControl.Resources>
	
	<Grid>
		<ItemsControl ItemsSource="{Binding Board.Vertices}" Background="#C0FFFFFF">
			<ItemsControl.ItemsPanel>
				<ItemsPanelTemplate>
					<comp:BoardPanel 
						IndexGetter="{Binding DataContext.IndexGetter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
						Offset="{Binding DataContext.Offset, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
				</ItemsPanelTemplate>
			</ItemsControl.ItemsPanel>
			<ItemsControl.ItemTemplate>
				<DataTemplate>
					<comp:Cell DataContext="{Binding}" Value="{Binding Value}" SetValueCommand="{Binding DataContext.SetValueCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
				</DataTemplate>
			</ItemsControl.ItemTemplate>
		</ItemsControl>

        <ItemsControl ItemsSource="{Binding Graph.Rows}" IsHitTestVisible="False" Visibility="{Binding ElementName=cbxRows, Path=IsChecked, Converter={StaticResource CP2VisibilityConverter}, ConverterParameter=True}">
			<ItemsControl.ItemsPanel>
				<ItemsPanelTemplate>
					<Canvas />
				</ItemsPanelTemplate>
			</ItemsControl.ItemsPanel>
			<ItemsControl.ItemTemplate>
				<DataTemplate>
					<Line Stroke="Green" StrokeThickness="3">
						<Line.X1>
							<MultiBinding Converter="{StaticResource RowLocationConverter}" Mode="OneWay">
								<Binding Path="From.X" />
								<Binding Path="DataContext.Offset.Item1" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
							</MultiBinding>
						</Line.X1>
						<Line.Y1>
							<MultiBinding Converter="{StaticResource RowLocationConverter}">
								<Binding Path="From.Y" />
								<Binding Path="DataContext.Offset.Item2" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
							</MultiBinding>
						</Line.Y1>
						<Line.X2>
							<MultiBinding Converter="{StaticResource RowLocationConverter}" Mode="OneWay">
								<Binding Path="To.X" />
								<Binding Path="DataContext.Offset.Item1" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
							</MultiBinding>
						</Line.X2>
						<Line.Y2>
							<MultiBinding Converter="{StaticResource RowLocationConverter}">
								<Binding Path="To.Y" />
								<Binding Path="DataContext.Offset.Item2" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
							</MultiBinding>
						</Line.Y2>
					</Line>
				</DataTemplate>
			</ItemsControl.ItemTemplate>
		</ItemsControl>

        <Border HorizontalAlignment="Left" VerticalAlignment="Top" TextBlock.Foreground="White" Padding="5,5,10,10" Visibility="{Binding State, Converter={StaticResource CP2VisibilityConverter}, ConverterParameter=InProgress}" CornerRadius="0,0,10,0">
            <Border.Background>
                <SolidColorBrush Color="Black" Opacity="0.65" />
            </Border.Background>

            <StackPanel>
                <TextBlock Text="You control:" />
                <CheckBox Content="X player" IsChecked="{Binding Board.ControlsXPlayer}" Foreground="White" />
                <CheckBox Content="O player" IsChecked="{Binding Board.ControlsOPlayer}" Foreground="White" />
                <Button Content="Refresh" Command="{Binding RefreshCommand}" Margin="0,8,0,0" />
                <Button Content="Restart" Command="{Binding StartGameCommand}" Margin="0,8,0,0" />
            </StackPanel>
        </Border>
        
        <Border HorizontalAlignment="Right" VerticalAlignment="Top" TextBlock.Foreground="White" Padding="10,5,5,10" Visibility="{Binding State, Converter={StaticResource CP2VisibilityConverter}, ConverterParameter=InProgress}" CornerRadius="0,0,0,10">
            <Border.Background>
                <SolidColorBrush Color="Black" Opacity="0.65" />
            </Border.Background>

            <StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <TextBlock Text="next:" />
                    <TextBlock Text="{Binding Board.NextValue, Converter={StaticResource CellValueConverter}}" Margin="4,0" />
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <CheckBox Content="rows:" Foreground="White" x:Name="cbxRows" IsChecked="False" />
                    <TextBlock Text="{Binding Graph.Rows.Count}" Margin="4,0" />
                </StackPanel>
                <CheckBox Content="show prob" Foreground="White" IsChecked="{Binding ShowProbabilities}" />
            </StackPanel>
        </Border>

        <Border Visibility="{Binding State, Converter={StaticResource CP2VisibilityConverter}, ConverterParameter=Stopped}" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="20" CornerRadius="10">
            <Border.Background>
                <SolidColorBrush Color="WhiteSmoke" Opacity="0.9" />
            </Border.Background>
            <StackPanel>
                <StackPanel Orientation="Horizontal" TextBlock.FontSize="16" TextBlock.FontWeight="Bold">
                    <TextBlock Text="Player" />
                    <TextBlock Text="{Binding Winner, Converter={StaticResource CellValueConverter}}" Margin="4,0" />
                    <TextBlock Text="won" />
                </StackPanel>
                <Button HorizontalAlignment="Center" Content="Play again" Margin="0,10,0,0" Command="{Binding StartGameCommand}" />
            </StackPanel>
        </Border>
	</Grid>
</UserControl>
